package com.baiyang.server.control.read;

import java.util.List;
import java.util.Map;

import com.baiyang.server.model.system.Author;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;

import com.baiyang.server.annotation.Jurisdiction;
import com.baiyang.server.model.read.Bookshelf;
import com.baiyang.server.model.PageEntity;
import com.baiyang.server.service.read.BookshelfService;
import com.baiyang.server.tools.Letter;
import com.baiyang.server.tools.DataTool;
import com.baiyang.server.tools.SubjectNotNull;

@RestController
@RequestMapping(value = "/bookshelfOperation")
@Jurisdiction(value = "/bookshelfOperation")
public class BookshelfCon {
	@Autowired
	private BookshelfService bookshelfService = null;

	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getPage")
	@RequestMapping(value = "/getPage",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Bookshelf> getPage(@RequestBody JSONObject object){
		Letter<Bookshelf> letter = new Letter<Bookshelf>();
		try {
			PageEntity<Bookshelf> result = bookshelfService.getPage(object);
			letter.setList(result.getDatas());
			letter.setTotal(result.getCount());
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}


	/**
	 * 获取关注列表
	 * @return
	 */
	@Jurisdiction(value = "getFollowList")
	@RequestMapping(value = "/getFollowList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Author> getFollowList(@RequestBody JSONObject object){
		Letter<Author> letter = new Letter<Author>();
		try {
			String readId = object.getString("userId");
			if(DataTool.isBlank(readId)){
				letter.setDetail("数据异常");
				letter.setResult("FAIL");
			}
			List<Author> authors = bookshelfService.getFollowList(readId);
			letter.setList(authors);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件分页获取数据
	 * @return
	 */
	@Jurisdiction(value = "getFollowState")
	@RequestMapping(value = "/getFollowState",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Boolean> getFollowState(@RequestBody JSONObject object){
		Letter<Boolean> letter = new Letter<Boolean>();
		try {
			String readId = object.getString("userId");
			String articleId = object.getString("articleId");
			if(DataTool.isBlank(readId)||DataTool.isBlank(articleId)){
				letter.setDetail("数据异常");
				letter.setResult("FAIL");
			}
			Boolean state = bookshelfService.getFollowState(readId,articleId);
			letter.setData(state);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

     /**
	 * 根绝条件获取数据
	 * @return
	 */
    @Jurisdiction(value = "selectList")
	@RequestMapping(value = "/selectList",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Bookshelf> selectList(@RequestBody Bookshelf bookshelf){
		Letter<Bookshelf> letter = new Letter<Bookshelf>();
		try {

			List<Bookshelf> result = bookshelfService.selectList(bookshelf);
			letter.setList(result);
			letter.setDetail("数据获取成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 根据条件获取一个
	 * @param bookshelf
	 * @return
	 */
	@Jurisdiction(value = "selectOne")
	@RequestMapping(value = "/selectOne",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<Bookshelf> selectOne(@RequestBody Bookshelf bookshelf){
		Letter<Bookshelf> letter = new Letter<Bookshelf>();
		try {

			Bookshelf obj = bookshelfService.selectOne(bookshelf);
			letter.setData(obj);
			letter.setDetail("数据获取成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据获取失败！");
		}
		return letter;
	}

	/**
	 * 插入数据
	 * @param object
	 * @return
	 */
	@Jurisdiction(value = "follow")
	@RequestMapping(value = "/follow",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> follow(@RequestBody JSONObject object){
		Letter<String> letter = new Letter<String>();
		String readId = object.getString("userId");
		String articleId = object.getString("articleId");
		if(DataTool.isBlank(readId)||DataTool.isBlank(articleId)){
			letter.setDetail("数据异常");
			letter.setResult("FAIL");
		}

		try {
			bookshelfService.follow(articleId,readId);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 插入数据
	 * @param bookshelf
	 * @return
	 */
	@Jurisdiction(value = "insert")
	@RequestMapping(value = "/insert",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insert(@RequestBody Bookshelf bookshelf){
		Letter<String> letter = new Letter<String>();
		bookshelf.setId(DataTool.createUUID());
		Map<String , String> sub = SubjectNotNull.subject(bookshelf);
		if(sub.size() != 0) {
			letter.setResult("FAIL");
			letter.setDetail("必填信息不能为空！" + sub.toString());
			return letter;
		}
		try {
			bookshelfService.insert(bookshelf);
			letter.setDetail("数据插入成功！");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 批量插入数据
	 * @param bookshelfs
	 * @return
	 */
	@Jurisdiction(value = "insertBatch")
	@RequestMapping(value = "/insertBatch",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> insertBatch(@RequestBody List<Bookshelf> bookshelfs){
		Letter<String> letter = new Letter<String>();
		try {
			bookshelfService.insertBatch(bookshelfs,letter);
			letter.setResult("SUCCESS");
			letter.setDetail("数据插入成功！");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据插入失败！");
		}
		return letter;
	}

	/**
	 * 根绝条件进行删除
	 * @param bookshelf
	 * @return
	 */
	@Jurisdiction(value = "delete")
	@RequestMapping(value = "/delete",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> delete(@RequestBody Bookshelf bookshelf){
		Letter<String> letter = new Letter<String>();
		if(SubjectNotNull.subjectIsNull(bookshelf)) {
			letter.setResult("FAIL");
			letter.setDetail("信息不能为空！");
			return letter;
		}
		try {
			bookshelfService.delete(bookshelf);
			letter.setDetail("数据删除成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据删除失败！");
		}
		return letter;
	}
    /**
    * 根据id批量删除
    */
    @Jurisdiction(value = "deletes")
    @RequestMapping(value = "/deletes",produces = MediaType.APPLICATION_JSON_VALUE)
    public Letter<String> deletes(@RequestBody List<Bookshelf> bookshelfs){
        Letter<String> letter = new Letter<String>();
        if(DataTool.isBlank(bookshelfs)) {
            letter.setResult("FAIL");
            letter.setDetail("信息不能为空！");
            return letter;
        }
        try {
			bookshelfService.deletes(bookshelfs);
            letter.setDetail("数据删除成功");
            letter.setResult("SUCCESS");
        }catch (Exception e) {
            e.printStackTrace();
            letter.setResult("ERROR");
            letter.setDetail("数据删除失败！");
        }
        return letter;
    }

	/**
	 * 根绝id更新
	 * @param bookshelf
	 * @return
	 */
	@Jurisdiction(value = "updateById")
	@RequestMapping(value = "/updateById",produces = MediaType.APPLICATION_JSON_VALUE)
	public Letter<String> updateById(@RequestBody Bookshelf bookshelf){
		Letter<String> letter = new Letter<String>();
		if(bookshelf.getId().equals("")) {
			letter.setResult("FAIL");
			letter.setDetail("id不能为空！");
			return letter;
		}
		try {
			bookshelfService.updateById(bookshelf);
			letter.setDetail("数据修改成功");
			letter.setResult("SUCCESS");
		}catch (Exception e) {
			e.printStackTrace();
			letter.setResult("ERROR");
			letter.setDetail("数据修改失败！");
		}
		return letter;
	}
}
